package com.gillion.grule.client.SimilarityReckon;

import com.alibaba.fastjson.JSONArray;
import java.util.List;
import java.util.Map;

public class SimilarityReckon {
    public static List queryDocList(String keysStr, JSONArray list, JSONArray listAll) {
        String[] keys = keysStr.split(",");
        for (Object obj : list) {
            Map<String, Object> data = (Map<String, Object>)obj;
            String mes = "";
            for (String key : keys)
                mes = mes + data.get(key).toString();
            data.put("compareValue", mes);
        }
        for (Object obj : listAll) {
            Map<String, Object> data = (Map<String, Object>)obj;
            String mes = "";
            for (String key : keys)
                mes = mes + data.get(key).toString();
            data.put("compareValue", mes);
        }
        for (Object obj : list) {
            Map<String, Object> myVo = (Map<String, Object>)obj;
            myVo.put("source", Float.valueOf(0.0F));
            for (Object obj2 : listAll) {
                if (obj2 == obj)
                    continue;
                Map<String, Object> myVo2 = (Map<String, Object>)obj2;
                String str1 = myVo2.get("compareValue").toString();
                String str2 = (myVo.get("compareValue") == null) ? "" : myVo.get("compareValue").toString();
                if (null == str1 || null == str2)
                    str1 = "";
                int len1 = str1.length();
                int len2 = str2.length();
                int[][] dif = new int[len1 + 1][len2 + 1];
                int a;
                for (a = 0; a <= len1; a++)
                    dif[a][0] = a;
                for (a = 0; a <= len2; a++)
                    dif[0][a] = a;
                for (int k = 1; k <= len1; k++) {
                    for (int j = 1; j <= len2; j++) {
                        int temp;
                        if (str1.charAt(k - 1) == str2.charAt(j - 1)) {
                            temp = 0;
                        } else {
                            temp = 1;
                        }
                        dif[k][j] = min(new int[] { dif[k - 1][j - 1] + temp, dif[k][j - 1] + 1, dif[k - 1][j] + 1 });
                    }
                }
                float similarity = (1.0F - dif[len1][len2] / Math.max(str1.length(), str2.length())) * 100.0F;
                if (similarity > ((Float)myVo.get("source")).floatValue())
                    myVo.put("source", Float.valueOf(similarity));
            }
        }
        return (List)list;
    }

    private static int min(int... is) {
        int min = Integer.MAX_VALUE;
        for (int i : is) {
            if (min > i)
                min = i;
        }
        return min;
    }

    public static void main(String[] args) {}
}
